package com.xdf.docmanagementsystem.common.util;

import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @program: doc-management-system
 * @description: IO操作
 * @author: Zhang
 * @create: 2020-06-19 20:03
 **/
public class IOUtil {

    /**
     * 输出到文件
     *
     * @param path
     * @param fileName
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static File writeLocalDir(String path, String fileName, InputStream inputStream) throws IOException {
        File dir = new File(path);
        if (!dir.exists()) dir.mkdirs();
        File file = new File(dir.getPath() + File.separator + fileName);
        if (!file.exists()) file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        byte[] bytes = new byte[1024];
        int index;
        while ((index = inputStream.read(bytes)) != -1) {
            fos.write(bytes);
            fos.flush();
        }
        if (inputStream != null) {
            inputStream.close();
        }
        if (fos != null) {
            fos.close();
        }
        return dir;
    }

    /**
     * 获取当前项目的目录
     *
     * @return
     */
    public static String getAppHome() {
        ApplicationHome applicationHome = new ApplicationHome();
        File jar = applicationHome.getSource();
        return jar.getParentFile().getPath();
    }

    /**
     * 删除文件夹下所有文件
     *
     * @param path
     * @return
     */
    public static boolean deleteFilesInDir(String path) {
        File file = new File(path);
        if (!file.exists()) return false;
        if (!file.isDirectory()) return false;
        File[] list = file.listFiles();
        for (File f : list) {
            f.delete();
        }
        return true;
    }

    /**
     * 输出文件
     *
     * @param file
     * @param os
     */
    public static void writeFile(File file, OutputStream os) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        while (fileInputStream.read(bytes) != -1) {
            os.write(bytes);
            os.flush();
        }
        if (os != null) {
            os.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
    }

    /**
     * 转换为File文件
     *
     * @param multipartFile
     * @return
     */
    public static File multipartFileToFile(MultipartFile multipartFile) {
        File file = null;
        InputStream inputStream;
        OutputStream outputStream;
        if (multipartFile.equals("") || multipartFile.getSize() <= 0) {
            return null;
        }
        try {
            inputStream = multipartFile.getInputStream();
            file = new File(multipartFile.getOriginalFilename());
            outputStream = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
        } catch (IOException e) {
            return null;
        }

        return file;
    }

}
